package org.minuframework.mybatis;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.minuframework.exception.ExceptionFailed;
import org.minuframework.lang.StringUtils;
import org.minuframework.utils.LangUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;

/**
 * @author 米mi
 * @param  <J> ？ extends JsonResult
 * @param  <M> mybatis xml映射接口
 * @param  <T> mybatis 实体映射类
 */
public abstract class IServiceImpl<J extends JsonResult, M extends BaseMapper<T>, T> extends AbstractServieImpl<J> implements IService<J, T> {
    
    @Autowired
    protected M baseMapper;
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public T selectById(Serializable id) {
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("id", id);
        return baseMapper.selectByMap(paramMap);
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public T selectById(Serializable id, String name) {
        if (StringUtils.isEmpty(name)) { return selectById(id); }
        
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put(name, id);
        return baseMapper.selectByMap(paramMap);
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public T selectById(String[] keys, Object[] values) throws ExceptionFailed {
        if (keys == null || values == null || keys.length != values.length) { throw new ExceptionFailed("参数数量不匹配"); }
        Map<String, Object> params = Maps.newHashMap();
        for (int i = 0; i < keys.length; i++) {
            params.put(keys[i], values[i]);
        }
        if (params == null || params.isEmpty()) { throw new ExceptionFailed("参数不能为空"); }
        return baseMapper.selectByMap(params);
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int selectCount() {
        Map<String, Object> paramMap = Maps.newHashMap();
        return baseMapper.selectCountByMap(paramMap);
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int selectCountById(Serializable id) {
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("id", id);
        return baseMapper.selectCountByMap(paramMap);
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int selectCountById(Serializable id, String name) {
        if (StringUtils.isEmpty(name)) { return selectCountById(id); }
        
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put(name, id);
        return baseMapper.selectCountByMap(paramMap);
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int selectCountById(String[] keys, Object[] values) throws ExceptionFailed {
        if (keys == null || values == null || keys.length != values.length) { throw new ExceptionFailed("参数数量不匹配"); }
        Map<String, Object> params = Maps.newHashMap();
        for (int i = 0; i < keys.length; i++) {
            params.put(keys[i], values[i]);
        }
        if (params == null || params.isEmpty()) { throw new ExceptionFailed("参数不能为空"); }
        return baseMapper.selectCountByMap(params);
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public J save(Map<String, Object> params) throws ExceptionFailed {
        if (params == null || params.isEmpty()) { return renderError("参数不能为空"); }
        
        if (baseMapper.insertByMap(params)) {
            return renderSuccess();
        } else {
            return renderError();
        }
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public J delete(Map<String, Object> params) {
        if (params == null || params.isEmpty()) { return renderError("找不到记录或该记录已被删除"); }
        
        if (baseMapper.deleteByMap(params)) {
            return renderSuccess();
        } else {
            return renderError();
        }
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public J deleteBatch(Collection<? extends Serializable> list) {
        if (list == null || list.isEmpty()) { return renderError("找不到记录或该记录已被删除"); }
        
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("list", paramMap);
        
        if (baseMapper.deleteByMap(paramMap)) {
            return renderSuccess();
        } else {
            return renderError();
        }
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public J deleteBatch(Collection<? extends Serializable> list, String name) {
        if (list == null || list.isEmpty()) { return renderError("找不到记录或该记录已被删除"); }
        
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put(name, paramMap);
        
        if (baseMapper.deleteByMap(paramMap)) {
            return renderSuccess();
        } else {
            return renderError();
        }
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public J update(Map<String, Object> params) throws ExceptionFailed {
        if (params == null || params.isEmpty()) { return renderError("参数不能为空"); }
        
        if (baseMapper.updateByMap(params)) {
            return renderSuccess();
        } else {
            return renderError();
        }
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public J updateStatus(Map<String, Object> params) {
        if (params == null || params.isEmpty()) { return renderError("参数不能为空"); }
        
        if (baseMapper.updateByMap(params)) {
            return renderSuccess();
        } else {
            return renderError();
        }
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public J select(Map<String, Object> params) {
        if (params == null || params.isEmpty()) { return renderError("参数不能为空"); }
        
        T param = baseMapper.selectByMap(params);
        if (param != null) {
            return renderSuccess(param);
        } else {
            return renderError("找不到记录");
        }
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public J selectList(Map<String, Object> params) {
        if (params == null || params.isEmpty()) { return renderError("参数不能为空"); }
        
        List<T> param = baseMapper.selectListByMap(params);
        if (param != null) {
            return renderSuccess(param);
        } else {
            return renderError("找不到记录");
        }
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public J selectByPage(Map<String, Object> params) {
        if (params == null || params.isEmpty()) { return renderError("参数不能为空"); }
        
        Integer pageNum = params.containsKey("pageNum") ? LangUtils.parseInt(params.get("pageNum")) : 1;
        Integer pageSize = params.containsKey("pageSize") ? LangUtils.parseInt(params.get("pageSize")) : 15;
        
        PageHelper.startPage(pageNum, pageSize);
        List<T> list = baseMapper.selectListByMap(params);
        if (!list.isEmpty()) {
            PageInfo<T> pageInfo = new PageInfo<T>(list);
            return renderSuccess(pageInfo.getList(), pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(), pageInfo.getPageSize());
        } else {
            return renderError("找不到记录");
        }
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public J selectByPage(Map<String, Object> params, String[] keys) {
        if (params == null || params.isEmpty()) { return renderError("参数不能为空"); }
        if (keys == null || keys.length <= 0) { return selectByPage(params); }
        
        Integer pageNum = params.containsKey(keys[0]) ? LangUtils.parseInt(params.get(keys[0])) : 1;
        Integer pageSize = params.containsKey(keys[1]) ? LangUtils.parseInt(params.get(keys[1])) : 15;
        
        PageHelper.startPage(pageNum, pageSize);
        List<T> list = baseMapper.selectListByMap(params);
        if (!list.isEmpty()) {
            PageInfo<T> pageInfo = new PageInfo<T>(list);
            return renderSuccess(pageInfo.getList(), pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(), pageInfo.getPageSize());
        } else {
            return renderError("找不到记录");
        }
    }
    
}
